﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuocTeViet.DO;
using QuocTeViet.BL;

namespace QuocTeViet
{
    public partial class Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load();
                LoadListItem();
            }
        }

        public string description = string.Empty;
        public void LoadListItem()
        {
            int id = Convert.ToInt32(Request.QueryString["Id"]);
            cmsLoaiSanPhamDO itemDO = new cmsLoaiSanPhamDO();
            itemDO.LoaiSanPhamID = id;
            itemDO = new cmsLoaiSanPhamBL().Select(itemDO);
            if (itemDO.MoTa!=null)
                description = itemDO.eMoTa.ToString();
            if(itemDO.TenLoaiSanPham!=null)
                lblTitle.Text = itemDO.TenLoaiSanPham.ToString();
            DataTable list = new cmsLoaiSanPhamBL().SelectByParent(id);
            rpList.DataSource = list;
            rpList.DataBind();
        }
        public void load()
        {

            Page.Title = "Danh mục xe" + " - " +  new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;
            DataTable dt = new DataTable();
            if (Request.QueryString["ID"] != null)
            {
                if (Request.QueryString["ID"].ToString() == "SanPhamMoi")
                    dt = new cmsSanPhamBL().SelectMoi(0);
                else if (Request.QueryString["ID"].ToString() == "SanPhamNoiBat")
                    dt = new cmsSanPhamBL().SelectNoiBat(0);
                else
                {
                    try
                    {
                        loadSlide();
                        string temp = Request.QueryString["ID"];
                        dt = new cmsSanPhamBL().SelectByLoaiAnhNhanHieu(int.Parse(Request.QueryString["ID"].ToString()), 0);
                        //ucSlideSP.Visible = true;
                        Page.Title = new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = int.Parse(Request.QueryString["ID"].ToString()) }).TenLoaiSanPham + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;
                    }
                    catch
                    {
                        string t = Request.QueryString["ID"].ToString();
                        dt = new cmsSanPhamBL().SelectByLoaiAnhNhanHieu(0, int.Parse(t.Substring(t.LastIndexOf("-")+1)));
                    }
                }
            }
            else if (Request.QueryString["IDNH"] != null)
            {
                dt = new cmsSanPhamBL().SelectByLoaiAnhNhanHieu(0, int.Parse(Request.QueryString["IDNH"].ToString()));
            }
            else
                dt = new cmsSanPhamBL().SelectAll();
            bindatalist(dt);
        }
        public void loadSlide()
        {
            //try
            //{
            //    if (Request.QueryString["ID"].ToString() != null)
            //    {
            //        int id = int.Parse(Request.QueryString["ID"].ToString());
            //        DataTable dt = new SlideBL().SelectByCate(id);
            //        if (dt.Rows.Count > 0)
            //        {
            //            rptSlide.DataSource = dt;
            //            rptSlide.DataBind();
            //        }
            //        else
            //            rptSlide.Visible = false;
            //    }
            //}
            //catch (Exception ee)
            //{
            //    string s = ee.ToString();
            //    rptSlide.Visible = false;
            //}
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }

        protected void ddlPhanLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dtPhanLoai = new cmsSanPhamBL().cmsSanPham_SelectByPhanLoai(int.Parse(ddlPhanLoai.SelectedValue));

            //bindatalist(dtPhanLoai);
 
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rptSanPham.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 9;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rptSanPham.Visible = true;
                rptSanPham.DataSource = objPds;
                rptSanPham.DataBind();
            }
            else
            {
                lbThongbao.Visible = true;
                lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }
        public string WordCut(string text)
        {
            return Ultility.WordCut(text, 100, new char[] { ' ', '.', ',', ';' }) + "...";

        }
        public string WordCutTitle(string text)
        {
            return Ultility.WordCut(text, 45, new char[] { ' ', '.', ',', ';' }) + "";

        }
        protected void rptSanPham_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0)
            {
                return;
            }
            DataRowView itemData = (DataRowView)e.Item.DataItem;
            Panel pnlHot = (Panel)e.Item.FindControl("pnlHot");
            Panel pnlNew = (Panel)e.Item.FindControl("pnlNew");
            if (Convert.ToBoolean(itemData["SanPhamMoi"]) == true)
            {
                pnlHot.Visible = false;
                pnlNew.Visible = true;
            }
            else if (Convert.ToBoolean(itemData["SanPhamBanChay"]) == true)
            {
                pnlHot.Visible = true;
                pnlNew.Visible = false;
            }
            else
            {
                pnlHot.Visible = false;
                pnlNew.Visible = false;
            }
        }
    }
}
